//
//  LYMineIntrodutionViewController.m
//  artist
//
//  Created by Jay_Apple on 16/6/1.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYMineIntrodutionViewController.h"
#import "LYIntoductionModel.h"
#import "LYIntrodutionCell.h"
#import "LYStudentInfoModel.h"

@interface LYMineIntrodutionViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *models;

@end

@implementation LYMineIntrodutionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.models = [[LYDataSourceManager sharedManager] getIntroductionModels];
}


#pragma mark - 网络请求

- (void)sendIntrodutionMessage{

    LYUpdateInfoModel *model = [[LYUpdateInfoModel alloc] init];
    
    model.token = LYToken;
    model.role_id = [NSString stringWithFormat:@"%zd",[LYDataSourceManager sharedManager].userInfo.role_id];
    
    for (NSInteger i = 0; i < 3; i++) {
        LYIntoductionModel *introduction = self.models[i];
        
        if (i == 0) {
            model.intro = introduction.content;
        }else if(i == 1){
            model.achieve = introduction.content;
        }else{
            model.theories = introduction.content;
        }
        
    }
    
    [MBProgressHUD showMessage:nil];
    
    [LYRequestTool setPersonalInfo:model success:^{
        [MBProgressHUD hideHUD];
        [MBProgressHUD showSuccess:@"提交成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showOnlyText:message];
    }];
    


}



#pragma mark - 点击方法

- (IBAction)commit:(id)sender {
    
    for (LYIntoductionModel *model in self.models) {
        
        if (model.content == nil) {
            
            [MBProgressHUD showOnlyText:[NSString stringWithFormat:@"请输入%@",model.title]];
            return;
        }
        
    }
    
    
    [self sendIntrodutionMessage];
    
}



#pragma mark - tableviewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"intodutionCell";
    
    LYIntrodutionCell *cell = [LYIntrodutionCell cellWithTableView:tableView andIndetifier:cellID];
    cell.model = self.models[indexPath.section];
    
    return cell;
}

#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 25;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    LYIntoductionModel *model = self.models[section];
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth, 25)];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, header.My_Width, header.My_Height)];
    title.font = [UIFont systemFontOfSize:13];
    title.textColor = [UIColor colorWithHexString:@"#A9A9A9" alpha:1.0];
    title.text = model.title;
    [header addSubview:title];
    
    return header;

}



@end
